package com.easy.component.utils;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;

import java.io.File;

import static android.app.Activity.RESULT_OK;

public class ImageSelect {
    /**
     *
     */
    private Activity activity;
    /**
     * 拍照照片路径
     */
    private File cameraSavePath;
    /**
     *
     */
    private Uri uri;
    /**
     * 返回
     */
    private PhotoPathCallBackLister photoPathCallBackLister;

    public ImageSelect(Activity activity, PhotoPathCallBackLister photoPathCallBackLister) {
        this.activity = activity;
        this.photoPathCallBackLister = photoPathCallBackLister;
    }

    //激活相册操作
    public void goPhotoAlbum() {
        try {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_PICK);
            intent.setType("image/*");
            activity.startActivityForResult(intent, 2);
        } catch (Exception e) {

        }
    }

    //激活相机操作
    public void goCamera() {
        try {
            String externalCacheDir = activity.getExternalCacheDir().getAbsolutePath();
            File file = new File(externalCacheDir);
            if (!file.isDirectory()) {
                file.mkdir();
            }
            cameraSavePath = new File(externalCacheDir + "/" + System.currentTimeMillis() + ".jpg");

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                uri = FileProvider.getUriForFile(activity, AppUtils.getPackageName(activity) + ".fileprovider", cameraSavePath);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            } else {
                uri = Uri.fromFile(cameraSavePath);
            }
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            activity.startActivityForResult(intent, 1);
        } catch (Exception e) {

        }

    }

    /**
     * 回调
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1 && resultCode == RESULT_OK) {
            String photoPath;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                photoPath = String.valueOf(cameraSavePath);
            } else {
                photoPath = uri.getEncodedPath();
            }
            if (null != photoPathCallBackLister) {
                photoPathCallBackLister.callBackPhotoPath(photoPath);
            }
        } else if (requestCode == 2 && resultCode == RESULT_OK) {
            String photoPath = PhotoFromPhotoAlbum.getRealPathFromUri(activity, data.getData());
            if (null != photoPathCallBackLister) {
                photoPathCallBackLister.callBackPhotoPath(photoPath);
            }
        }
    }

    public interface PhotoPathCallBackLister {
        void callBackPhotoPath(String photoPath);
    }
}
